#!/usr/bin/perl

my $repo       = "gutenberg_scifi";
my $template   = "templates/project_gutenberg_repo.xml";
my $plist_dir  = "$repo/plists";
my $repository = "$repo/index.xml";
my $host       = "www.pollock.ca";
my $html_base  = "/var/www/html";
my $s3_host    = "library.pollock.ca";

opendir(DIR, $plist_dir) || die "can't opendir $plist_dir: $!";
@files = grep { /\.plist/ && -f "$plist_dir/$_" } readdir(DIR);
closedir DIR;

open TEMPLATE, "<$template" or die "Unable to open $template for reading $!";
open OUT_FILE, ">$repository" or die "Unable to open $repository for writing $!";

while (<TEMPLATE>) {
    my $line = $_;

    if ($line =~ /PLISTS_HERE/ ) {
	foreach my $file (@files) {
	    open IN_FILE, "<$plist_dir/$file";
	    while (<IN_FILE>) {
		print OUT_FILE $_;
	    }
	    close IN_FILE;
	}
    } else {
	print OUT_FILE $line;
    }
}

close OUT_FILE;
close TEMPLATE;

system("scp $repository $host:$html_base/$repository");
print "cmd: s3cmd.rb put $s3_host:$repo $repository x-amz-acl:public-read\n";
system("s3cmd.rb put $s3_host:$repo $repository x-amz-acl:public-read");
print "s3cmd.rb put $s3_host:$repo/gutenmark-iphone.20080526.zip GutenMark/gutenmark-iphone.20080526.zip x-amz-acl:public-read\n";
system("s3cmd.rb put $s3_host:$repo/gutenmark-iphone.20080526.zip GutenMark/gutenmark-iphone.20080526.zip x-amz-acl:public-read");
